// 环境配置
const env = process.env.NODE_ENV || 'development'

// 环境配置对象
const EnvConfig = {
  development: {
    baseApi: '/api',
    uploadApi: '/upload',
    mockApi: '/mock',
    wsApi: '/ws',
    // 开发环境后端地址
    backendUrl: 'http://localhost:8080',
    // 是否启用 Mock
    mockEnabled: false
  },
  production: {
    baseApi: 'https://your-production-api.com/api',
    uploadApi: 'https://your-production-api.com/upload',
    mockApi: '',
    wsApi: 'wss://your-production-api.com/ws',
    // 生产环境后端地址
    backendUrl: 'https://your-production-api.com',
    // 生产环境不启用 Mock
    mockEnabled: false
  },
  test: {
    baseApi: 'https://your-test-api.com/api',
    uploadApi: 'https://your-test-api.com/upload',
    mockApi: '',
    wsApi: 'wss://your-test-api.com/ws',
    // 测试环境后端地址
    backendUrl: 'https://your-test-api.com',
    // 测试环境不启用 Mock
    mockEnabled: false
  }
}

// 当前环境配置
const currentEnv = EnvConfig[env] || EnvConfig.development

// 环境配置对象（兼容旧版本）
export const ENV_CONFIG = {
  // API基础URL
  API_BASE_URL: currentEnv.backendUrl,
  
  // DeepSeek AI API配置
  DEEPSEEK_API_KEY: process.env.VITE_DEEPSEEK_API_KEY || '',
  DEEPSEEK_BASE_URL: process.env.VITE_DEEPSEEK_BASE_URL || 'https://api.deepseek.com',
  
  // 应用配置
  APP_NAME: process.env.VITE_APP_NAME || '法律援助平台',
  APP_VERSION: process.env.VITE_APP_VERSION || '1.0.0',
  
  // 开发环境配置
  IS_DEV: env === 'development',
  IS_PROD: env === 'production',
  
  // 功能开关
  ENABLE_AI: process.env.VITE_ENABLE_AI !== 'false',
  ENABLE_MOCK: currentEnv.mockEnabled,
  
  // 调试配置
  DEBUG_MODE: process.env.VITE_DEBUG_MODE === 'true',
  
  // 请求超时
  REQUEST_TIMEOUT: 10000
}

// 检查必要的环境变量
export const validateEnv = () => {
  const requiredVars = [
    'VITE_DEEPSEEK_API_KEY'
  ]
  
  const missingVars = requiredVars.filter(varName => !process.env[varName])
  
  if (missingVars.length > 0) {
    console.warn('⚠️ 缺少环境变量:', missingVars.join(', '))
    console.warn('请检查 .env 文件配置')
  }
  
  return missingVars.length === 0
}

// 获取API配置
export const getApiConfig = () => ({
  baseURL: ENV_CONFIG.API_BASE_URL,
  timeout: ENV_CONFIG.REQUEST_TIMEOUT,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 获取AI配置
export const getAIConfig = () => ({
  apiKey: ENV_CONFIG.DEEPSEEK_API_KEY,
  baseURL: ENV_CONFIG.DEEPSEEK_BASE_URL,
  model: 'deepseek-chat',
  maxTokens: 1000,
  temperature: 0.7
})

// 导出默认配置
export default {
  env,
  mock: currentEnv.mockEnabled,
  namespace: 'law-aid-platform',
  ...currentEnv
} 